Skip to content

[Backend] Implement Environment Config Validation Guard#315

Open
armorbreak001 wants to merge 1 commit intoGalactiGuild:mainfrom
armorbreak001:bounty/292-env-config-validation
Open

[Backend] Implement Environment Config Validation Guard#315
armorbreak001 wants to merge 1 commit intoGalactiGuild:mainfrom
armorbreak001:bounty/292-env-config-validation

Conversation

@armorbreak001
Copy link
Copy Markdown

Fixes #292

Changes

  • package.json: Added joi dependency
  • AppModule.ConfigModule: Added validationSchema using Joi
    • PORT: number, defaults to 3000
    • DATABASE_URL: required — connection string for PostgreSQL
    • JWT_SECRET: required, minimum 16 characters (with custom error message)
    • REDIS_HOST/PORT/URL: optional (for queue interactions)
    • abortEarly: false — reports all validation errors at once, not just the first
    • allowUnknown: true — permits additional env vars without error

Behavior

On startup, if any required env var is missing or invalid:

  • NestJS throws a clear error: "Missing Configuration: JWT_SECRET" or similar
  • The process crashes immediately (process.exit(1) handled by NestJS bootstrap)
  • No silent failures or cryptic runtime errors later

Acceptance Criteria Met

  • Add joi to validate @nestjs/config module payload via validationSchema
  • Require PORT, DATABASE_URL, and JWT_SECRET strictly
  • REDIS fields are optional (queue can be disabled)
  • Hard fatal crash with clear "Missing Configuration: XYZ" on failure

How to Verify

  1. Remove JWT_SECRET from .env
  2. Run npm run start:dev
  3. Observe immediate crash with: "Missing Configuration: JWT_SECRET"
  4. Restore JWT_SECRET → server starts normally

- Add joi dependency for schema validation
- Add validationSchema to ConfigModule.forRoot in AppModule
- Required: PORT (default 3000), DATABASE_URL, JWT_SECRET (min 16 chars)
- Optional: REDIS_HOST, REDIS_PORT, REDIS_URL
- On missing/invalid env vars → NestJS hard crash with clear error message
- abortEarly: false to show all validation errors at once

Fixes GalactiGuild#292
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Backend] Implement Environment Config Validation Guard (Joi/Zod)

1 participant